package com.wlyy.bcwlw.common.exception;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import com.wlyy.bcwlw.common.utils.WebMessage;


public class GlobalExceptionResolver implements HandlerExceptionResolver{
	
	private static final Logger logger = LogManager.getLogger(GlobalExceptionResolver.class);

	@Override
	public ModelAndView resolveException(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex) {
		// TODO Auto-generated method stub
		logger.error("统一异常处理", ex);
		if(ex instanceof AuthenticationFailedException) {
			//认证失败
			return new ModelAndView("/common/error/forbidden");
		}
		if(ex instanceof BadSqlGrammarException) {
			WebMessage.setErrorMessage(request, "输入参数有误，请输入正确的参数值");
		}else{
			WebMessage.setErrorMessage(request, "异常！原因：" + ex.getMessage());
		
		}
		return new ModelAndView("/common/error/showmessage");
	}

}
